package jp.co.dailyReport.weeklyReport.sql;

import java.util.Calendar;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.weeklyReport.WeeklyReportWorkItem;

/**
 * 週報作業項目情報を登録するクラスです。
 */
public class WeeklyReportWorkItemInsertExecuter implements Executer {

	private static Logger log = Logger.getLogger(WeeklyReportWorkItemInsertExecuter.class);
	private WeeklyReportWorkItem weeklyReportWorkItem;

	public WeeklyReportWorkItemInsertExecuter(WeeklyReportWorkItem weeklyReportWorkItem) {
		this.weeklyReportWorkItem = weeklyReportWorkItem;
	}

	/**
	 * 週報作業項目情報を登録します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "insert into weeklyReportWorkItem (workItemId,weeklyReportId,workingHours,upDay) values (?,?,?,?)";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,weeklyReportWorkItem.getWorkItemId());
			pstmt.setInt(2,weeklyReportWorkItem.getWeeklyReportId());
			pstmt.setDouble(3,weeklyReportWorkItem.getWorkingHours());
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(4,new Date(cal.getTime().getTime()));
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}
}